package com.wu.basic.dynamic.controller;

import com.wu.basic.dynamic.domain.uo.BaseSwipe;
import com.wu.framework.inner.layer.web.EasyController;
import com.wu.framework.inner.lazy.database.expand.database.persistence.LazyOperation;
import com.wu.framework.inner.lazy.database.expand.database.persistence.stream.lambda.LazyLambdaStream;
import com.wu.framework.inner.template.proxy.http.ProxyRestTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.boot.CommandLineRunner;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * description 刷单
 *
 * @author 吴佳伟
 * @date 2021/5/12 5:55 下午
 */
@Api(tags = "刷单")
@EasyController("/swipe")
public class SwipeController implements CommandLineRunner {

    private final LazyOperation lazyOperation;

    private final ProxyRestTemplate proxyRestTemplate;
    private final LazyLambdaStream lazyLambdaStream;

    public SwipeController(LazyOperation lazyOperation, ProxyRestTemplate proxyRestTemplate, LazyLambdaStream lazyLambdaStream) {
        this.lazyOperation = lazyOperation;
        this.proxyRestTemplate = proxyRestTemplate;
        this.lazyLambdaStream = lazyLambdaStream;
    }

    @ApiOperation(tags = "刷单", value = "csdn 文章刷单")
    @PostMapping("/csdn")
    public void csdn(BaseSwipe baseSwipe) {
        new Thread(() -> {
            lazyOperation.smartUpsert(baseSwipe);
            for (int i = 0; i < baseSwipe.getTimes(); i++) {
//                try {
//                    Thread.sleep(baseSwipe.getSleepTimes());
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
                String string = proxyRestTemplate.getForObject(baseSwipe.getUrl(), String.class);
//                System.out.println(string);
                baseSwipe.setCurrentTimes(i);
                lazyOperation.upsert(baseSwipe);
            }
            lazyOperation.upsert(baseSwipe.setIsActive(false));
        }).start();
    }

    @Override
    public void run(String... args) throws Exception {
        List<BaseSwipe> baseSwipes = new ArrayList<>(lazyLambdaStream.of(BaseSwipe.class).select(null).collection());

        for (BaseSwipe baseSwipe : baseSwipes) {
            new Thread(() -> {
                for (int i = baseSwipe.getCurrentTimes(); i < baseSwipe.getTimes(); i++) {
//                    try {
//                        Thread.sleep(baseSwipe.getSleepTimes());
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
                    String string = proxyRestTemplate.getForObject(baseSwipe.getUrl(), String.class);
//                    System.out.println(string);
                    baseSwipe.setCurrentTimes(i);
//                        lazyOperation.upsert(baseSwipe);
                }
                lazyOperation.upsert(baseSwipe.setIsActive(false));
            }).start();
        }
        System.out.println(baseSwipes);

    }
}
